/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.pages.secure;

import it.blueocean.acanto.web.AcantoPlatformApplication;
import it.blueocean.acanto.web.platform.components.header.CustomHeaderPanel;

import org.apache.wicket.Application;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Session;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.protocol.http.ClientProperties;
import org.apache.wicket.protocol.http.request.WebClientInfo;
import org.apache.wicket.security.WaspSession;
import org.apache.wicket.security.components.SecureWebPage;
import org.apache.wicket.security.hive.authentication.LoginContext;
import org.apache.wicket.security.swarm.SwarmWebApplication;
import org.apache.wicket.util.string.Strings;

public abstract class ASecurePage extends SecureWebPage
{
    public ASecurePage()
    {
        init();
    }

    /**
     * @param parameters
     */
    public ASecurePage(PageParameters parameters)
    {
        super(parameters);
        init();
    }

    /**
     * @param model
     */
    public ASecurePage(IModel<?> model)
    {
        super(model);
        init();
    }

    /**
     * initialize the page.
     */
    protected void init()
    {
    	add(CSSPackageResource.getHeaderContribution("/css/buttonpanel.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/inputcomponent.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/login.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/reset-fonts-grids.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/style.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/table.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/tabpanel.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/validation.css"));
    	add(CSSPackageResource.getHeaderContribution("/css/form.css"));
    	
    	add(JavascriptPackageResource.getHeaderContribution("js/freegas_custom.js"));
    	add(JavascriptPackageResource.getHeaderContribution("js/prototype.js"));
        
        Session session = RequestCycle.get().getSession();
        WebClientInfo clientInfo = (WebClientInfo) session.getClientInfo();
        ClientProperties clientProperties = clientInfo.getProperties();
        if(clientProperties.isBrowserInternetExplorer())
        {
        	add(CSSPackageResource.getHeaderContribution("css/ie.css"));
        	add(JavascriptPackageResource.getHeaderContribution("js/iehover.js"));
        }
        
        final String packageName = getClass().getPackage().getName();
        CustomHeaderPanel headerPanel = new CustomHeaderPanel("mainNavigation", Strings.afterLast(packageName, '.'), this);
        add(headerPanel);

        // not a secure link because everyone can logoff.
        Link<?> logoff = new Link<Object>("logoff")
        {
            private static final long serialVersionUID = 1L;

            public void onClick()
            {
                WaspSession waspSession = ((WaspSession) getSession());
                if (waspSession.logoff(getLogoffContext()))
                {
                    // homepage is not allowed anymore so we end up at the
                    // loginpage
                    setResponsePage(Application.get().getHomePage());
                    waspSession.invalidate();
                }
                else
                    error("A problem occured during the logoff process, please try again or contact support");
            }
        };

        add(logoff);
        
        setVersioned(false);
    }

    /**
     * shortcut to the {@link WaspSession}.
     * 
     * @return the session.
     */
    protected final WaspSession getSecureSession()
    {
        return (WaspSession) Session.get();
    }

    /**
     * Shortcut to the application.
     * 
     * @return the application
     */
    protected final SwarmWebApplication getSecureApplication()
    {
        return (SwarmWebApplication) Application.get();
    }

    /**
     * Allows subclasses to specify which context should be used when logging
     * off
     * 
     * @return the context
     */
    protected final LoginContext getLogoffContext()
    {
        Application app = Application.get();
        if (app instanceof AcantoPlatformApplication)
            return ((AcantoPlatformApplication) app).getLogoffContext();
        else
            throw new WicketRuntimeException(
                    "Application is not a subclass of "
                            + AcantoPlatformApplication.class);
    }
}
